home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 11587 / 11587.xpi / components / connect.js < prev   
Text File  |  2009-07-08  |  9KB  |  318 lines

  1. /* Copyright (c) 2008 Pearl Crescent, LLC.  All Rights Reserved. */
  2. /* vim: set sw=2 sts=2 ts=8 et syntax=javascript: */
  3.  
  4. var gAviaryConnect = {
  5.   kPearlUtilJSURI: "chrome://aviary/content/pearlutil.js",
  6.  
  7.   kCapturedImageExpireTimeMS: 300000, // 5 minutes (5 * 60 * 1000)
  8.   kMaxStackDepth: 250,
  9.  
  10.   // Use lowercase letters within mHosts.
  11.   mHosts: [ "aviary.com", "test.viary.com", "test.aviary.com" ],
  12.   mCurrentID: 0,
  13.   mCapturedImages: null,
  14.  
  15.   _mIOSvc: null,
  16.  
  17.   // nsISupports implementation.
  18.   QueryInterface: function (aIID)
  19.   {
  20.     if (!aIID.equals(Components.interfaces.nsISupports) &&
  21.         !aIID.equals(Components.interfaces.nsIFactory) &&
  22.         !aIID.equals(Components.interfaces.nsIClassInfo) &&
  23.         !aIID.equals(Components.interfaces.aviaryIConnectInternal) &&
  24.         !aIID.equals(Components.interfaces.aviaryIConnect))
  25.     {
  26.       throw Components.results.NS_ERROR_NO_INTERFACE;
  27.     }
  28.  
  29.     return this;
  30.   },
  31.  
  32.   // nsIFactory implementation.
  33.   createInstance: function (aOuter, aIID)
  34.   {
  35.     if (null != aOuter)
  36.       throw Components.results.NS_ERROR_NO_AGGREGATION;
  37.  
  38.     // Load Pearl Utility library.
  39.     var jsLoader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
  40.                    .createInstance(Components.interfaces.mozIJSSubScriptLoader);
  41.     jsLoader.loadSubScript(this.kPearlUtilJSURI);
  42.  
  43.     this.mPearlUtil = com.aviary.talon.pearlutil;
  44.  
  45.     return this.QueryInterface(aIID);
  46.   },
  47.  
  48.   lockFactory: function (aDoLock) {},
  49.  
  50.   // nsIClassInfo implementation.
  51.   getInterfaces: function(aCount)
  52.   {
  53.     // Whatever interfaces are listed here are available to web pages.
  54.     var iList = [ Components.interfaces.nsIClassInfo,
  55.                   Components.interfaces.aviaryIConnect ];
  56.     aCount.value = iList.length;
  57.     return iList;
  58.   },
  59.  
  60.   getHelperForLanguage: function (aLanguage)
  61.   {
  62.     return null;
  63.   },
  64.  
  65.   classDescription: "AviaryConnectAPI",
  66.   implementationLanguage: Components.interfaces.nsIProgrammingLanguage.JAVASCRIPT,
  67.   flags: Components.interfaces.nsIClassInfo.DOM_OBJECT,
  68.  
  69.   // aviaryIConnectInternal implementation.
  70.   addCapturedImage: function(aMimeType, aDataURI)
  71.   {
  72.     this.deleteOldImages();
  73.  
  74.     if (!aMimeType || !aDataURI)
  75.       throw Components.results.NS_ERROR_INVALID_ARG;
  76.  
  77.     ++this.mCurrentID;
  78. // dump("addCapturedImage: " + aMimeType + ", ID: " + this.mCurrentID + "\n");
  79.     var imageObj = new Object;
  80.     imageObj.id = this.mCurrentID;
  81.     imageObj.mimeType = aMimeType;
  82.     imageObj.dataURI = aDataURI;
  83.     imageObj.timeAddedMS = Date.now();
  84.  
  85.     if (!this.mCapturedImages)
  86.       this.mCapturedImages = new Object;
  87.     this.mCapturedImages[imageObj.id] = imageObj;
  88.  
  89.     return imageObj.id;
  90.   },
  91.  
  92.   // aviaryIConnect implementation.
  93.   hasClipboardData: function(aMimeType)
  94.   {
  95.     this.checkAccess("hasClipboardData");
  96.  
  97.     return this.mPearlUtil.HasClipboardData(aMimeType);
  98.   },
  99.  
  100.   getClipboardData: function(aMimeType)
  101.   {
  102.     this.checkAccess("getClipboardData");
  103.  
  104.     return this.mPearlUtil.GetClipboardAsDataURI(aMimeType);
  105.   },
  106.  
  107.   getCapturedImageMimeType: function(aID)
  108.   {
  109. // dump("getCapturedImageMimeType: " + aID + "\n");
  110.     this.checkAccess("getCapturedImageMimeType");
  111.  
  112.     // Delete old images first since we do not return a MIME type if we
  113.     // are about to delete the image.
  114.     this.deleteOldImages();
  115.  
  116.     if (aID && this.mCapturedImages)
  117.     {
  118.       var imageObj = this.mCapturedImages[aID];
  119.       if (imageObj)
  120.         return imageObj.mimeType;
  121.     }
  122.  
  123.     return null;
  124.   },
  125.  
  126.   getCapturedImage: function(aID, aDeleteIt)
  127.   {
  128. // dump("getCapturedImage: " + aID + ", delete id: " + aDeleteIt + "\n");
  129.     this.checkAccess("getCapturedImage");
  130.  
  131.     var dataURI = null;
  132.     if (aID && this.mCapturedImages)
  133.     {
  134.       var imageObj = this.mCapturedImages[aID];
  135.       if (imageObj)
  136.       {
  137.         dataURI = imageObj.dataURI;
  138.         if (aDeleteIt)
  139.           delete this.mCapturedImages[aID];
  140.       }
  141.     }
  142.  
  143.     this.deleteOldImages();
  144.     return dataURI;
  145.   },
  146.  
  147.   deleteCapturedImage: function(aID)
  148.   {
  149. // dump("deleteCapturedImage: " + aID + "\n");
  150.     this.checkAccess("deleteCapturedImage");
  151.  
  152.     if (aID && this.mCapturedImages)
  153.     {
  154.       var imageObj = this.mCapturedImages[aID];
  155.       if (imageObj)
  156.         delete this.mCapturedImages[aID];
  157.     }
  158.  
  159.     this.deleteOldImages();
  160.   },
  161.  
  162.   // internal functions.
  163.   deleteOldImages: function()
  164.   {
  165. // dump("deleteOldImages\n");
  166.     if (this.mCapturedImages)
  167.     {
  168.       var curTimeMS = Date.now();
  169.       for each (var imageObj in this.mCapturedImages)
  170.       {
  171. // dump("  checking image: " + imageObj.id + "\n");
  172.         if ((curTimeMS - imageObj.timeAddedMS) >
  173.             this.kCapturedImageExpireTimeMS)
  174.         {
  175. // dump("  deleting old image: " + imageObj.id + "\n");
  176.           delete this.mCapturedImages[imageObj.id];
  177.         }
  178.       }
  179.     }
  180.   },
  181.  
  182.   // Check that web page has access and throw an error if not.
  183.   checkAccess: function(aDesc)
  184.   {
  185.     var isURLOK = false;
  186.  
  187.     // Find top-most JS stack frame.
  188.     var stackFrame = Components.stack;
  189.     for (var i = 0; i < this.kMaxStackDepth; ++i)
  190.     {
  191.       if (!stackFrame.caller)
  192.         break;
  193.  
  194.       stackFrame = stackFrame.caller;
  195.     }
  196.  
  197.     // Verify that URL is white listed.
  198.     if (stackFrame && !stackFrame.caller)
  199.     {
  200. //      dump("checkAccess " + aDesc + "- URL: " + stackFrame.filename + "\n");
  201.       isURLOK = this.isURLWhiteListed(stackFrame.filename);
  202.     }
  203.  
  204. //    dump("checkAccess " + aDesc + " - " + (isURLOK ? "OK" : "Denied") + "\n");
  205.  
  206.     if (!isURLOK)
  207.     {
  208.       throw new Components.Exception("access denied",
  209.                                      Components.results.NS_ERROR_FAILURE);
  210.     }
  211.   },
  212.  
  213.   isURLWhiteListed: function(aURL)
  214.   {
  215.     if (!aURL)
  216.       return false;
  217.  
  218.     try
  219.     {
  220.       var uriObj = this.mIOSvc.newURI(aURL, null, null);
  221.       var scheme = uriObj.scheme;
  222.  
  223. //    if ("chrome" == scheme)
  224. //      return true; // allow access from all chrome pages.
  225.  
  226.       var lcHost = uriObj.host.toLowerCase();
  227.       if ("http" == scheme || "https" == scheme)
  228.       {
  229.         var len = this.mHosts.length;
  230.         for (var i = 0; i < len; ++i)
  231.         {
  232.           if (lcHost == this.mHosts[i])
  233.             return true; // allowed access from white listed host.
  234.         }
  235.       }
  236.     } catch (e) {}
  237.  
  238.     return false;
  239.   },
  240.  
  241.   get mIOSvc()
  242.   {
  243.     if (!this._mIOSvc)
  244.     {
  245.       this._mIOSvc = Components.classes["@mozilla.org/network/io-service;1"]
  246.                                .getService(Components.interfaces.nsIIOService);
  247.     }
  248.  
  249.     return this._mIOSvc;
  250.   },
  251.  
  252.   endOfObject: true
  253. };
  254.  
  255. var gAviaryConnectModule =
  256. {
  257.   kICompReg: Components.interfaces.nsIComponentRegistrar,
  258.   kClassID: Components.ID("{6FCCDA4C-4EA0-4204-B0AA-4E9546147AA0}"),
  259.   kContractID: "@aviary.com/aviary-api;1",
  260.   kDesc: "Aviary Connect API Module",
  261.   kJSName: "aviaryconnect",
  262.   kJSGlobalPropCategory: "JavaScript global property",
  263.  
  264.   // nsISupports implementation.
  265.   QueryInterface: function (aIID)
  266.   {
  267.     if (aIID.equals(Components.interfaces.nsIModule) ||
  268.         aIID.equals(Components.interfaces.nsISupports))
  269.       return this;
  270.  
  271.     throw Components.results.NS_ERROR_NO_INTERFACE;
  272.   },
  273.  
  274.   // nsIModule implementation.
  275.   getClassObject: function (aCompMgr, aClassID, aIID)
  276.   {
  277.     if (!aClassID.equals(this.kClassID))
  278.       throw Components.results.NS_ERROR_NO_INTERFACE;
  279.     if (!aIID.equals(Components.interfaces.nsIFactory))
  280.       throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  281.  
  282.     return gAviaryConnect.QueryInterface(aIID);
  283.   },
  284.  
  285.   registerSelf: function (aCompMgr, aFileSpec, aLocation, aType)
  286.   {
  287.     aCompMgr = aCompMgr.QueryInterface(this.kICompReg);
  288.     aCompMgr.registerFactoryLocation(this.kClassID, this.kDesc,
  289.                                  this.kContractID, aFileSpec, aLocation, aType);
  290.  
  291.     var catMgr = Components.classes["@mozilla.org/categorymanager;1"]
  292.                          .getService(Components.interfaces.nsICategoryManager);
  293.     catMgr.addCategoryEntry(this.kJSGlobalPropCategory, this.kJSName,
  294.                             this.kContractID, true, true);
  295.   },
  296.  
  297.   unregisterSelf: function (aCompMgr, aFileSpec, aLocation)
  298.   {
  299.     var catMgr = Components.classes["@mozilla.org/categorymanager;1"]
  300.                          .getService(Components.interfaces.nsICategoryManager);
  301.     catMgr.deleteCategoryEntry(this.kJSGlobalPropCategory, this.kJSName,
  302.                                true);
  303.     aCompMgr = aCompMgr.QueryInterface(this.kICompReg);
  304.     aCompMgr.unregisterFactoryLocation(this.kClassID, aFileSpec);
  305.   },
  306.  
  307.   canUnload: function (aCompMgr) { return true; },
  308.  
  309.   endOfObject: true
  310. };
  311.  
  312.  
  313. function NSGetModule(aCompMgr, aFileSpec)
  314. {
  315.   return gAviaryConnectModule;
  316. }
  317.  
  318.